package com.xzkydz.all_sensor.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.serialport.ComAssistant.Java_xor;
import com.xzkydz.all_sensor.activity.MainActivity;
import com.xzkydz.all_sensor.allsensor.R;

/**
 * Created by yk on 2016/10/17.
 */

public class WenShiDuFragment extends Fragment {

    String fuahoString = "00"; //正
    private EditText wenduEditText;
    private EditText daqiyaEditText;
    private Spinner fhSpinner1;
    private Spinner fhSpinner2;
    private Spinner bianhaoSPinner;
    private MainActivity mActivity;
    private EditText adaqiyaEditText;

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View  view  =  inflater.inflate(R.layout.fragment_wenshidu, container, false);
        mActivity = (MainActivity)getActivity();
        fhSpinner1 = (Spinner)view.findViewById(R.id.sp_fuhao);
        fhSpinner2 = (Spinner)view.findViewById(R.id.sp_fuaho_);
        bianhaoSPinner = (Spinner)view.findViewById(R.id.sp_bianhao);
        wenduEditText = (EditText)view.findViewById(R.id.et_wendu);
        daqiyaEditText = (EditText)view.findViewById(R.id.et_daqiya);
        adaqiyaEditText = (EditText)view.findViewById(R.id.et_a);
        Button wenduButton = (Button)view.findViewById(R.id.btn_wendu);
        Button daqiyaButton = (Button)view.findViewById(R.id.btn_daqiya);

        wenduButton.setOnClickListener(new MyOnClickListener());
        daqiyaButton.setOnClickListener(new MyOnClickListener());
        return view;
    }





    public  class  MyOnClickListener  implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_wendu:
                    if (wenduEditText.getText().toString().length()!=0){
                        String  ti  = "4B590A01050002100102";
                        if (fhSpinner1.getSelectedItemId()==0)  fuahoString = "00";
                        else  fuahoString = "01";
                        String  wenduString  = Integer.toHexString((int)(100*Float.parseFloat(wenduEditText.getText().toString())));
                        while (wenduString.length()!=4){wenduString = "0"+wenduString;}
                        String xorString = Java_xor.checkcode_0007("0A01050002100102" +fuahoString+ wenduString);
                        String data  = ti+fuahoString+ wenduString+xorString;
                        mActivity.ShowInf("\n\n\n"+"校准温度： "+fuahoString +  wenduString +"\n"+"完整校准数据包："+data+"\n\n\n");
                        mActivity.sendPortData(data);  //发送数据
                    }else {
                        Toast.makeText(getActivity(),"请输入校准值！",Toast.LENGTH_SHORT).show();
                    }
                break;
                case R.id.btn_daqiya:
                    if (daqiyaEditText.getText().toString().length()!=0 || adaqiyaEditText.getText().toString().length()!=0){
                        String  ti  = "4B590C01050002100103";
                        if (fhSpinner2.getSelectedItemId()==0)  fuahoString = "00";
                        else  fuahoString = "01";
                        String  adaqiyaString =  Integer.toHexString((int)(10000*Float.parseFloat(adaqiyaEditText.getText().toString())));
                        while (adaqiyaString.length()<2){adaqiyaString = "0"+adaqiyaString;}
                        String  daqiyaString  = Integer.toHexString((int)(Float.parseFloat(daqiyaEditText.getText().toString())));
                        while (daqiyaString.length()<4){daqiyaString = "0"+daqiyaString;}
                        String xorString = Java_xor.checkcode_0007("0C01050002100103" +adaqiyaString+fuahoString+ daqiyaString);
                        String data2  = ti+adaqiyaString+fuahoString+ daqiyaString+xorString;
                        mActivity.ShowInf("\n\n\n"+"校准大气压： "+fuahoString+   daqiyaString+"\n"+"完整校准数据包："+data2+"\n\n\n");
                        mActivity.sendPortData(data2);  //发送数据
                    }else {
                        Toast.makeText(getActivity(),"请输入校准值！",Toast.LENGTH_SHORT).show();
                    }
                break;
                default:
                    break;
            }
        }
    }

}
